﻿using System;
using System.Collections.Generic;
using System.Web;
using BaseSet.Framework.Config;
using BaseSet.Entity.Common.Text;
using BaseSet.Entity;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet;
using BaseSet.Data;

namespace WebSite
{
    public class UserBase
    {
        public static Users  GetCurrentAdminUser()
        {
            if (System.Web.HttpContext.Current.Session["CurrentAdminUser"] == null)
            {
                //读取cookie中的值
                HttpCookie cookies = System.Web.HttpContext.Current.Request.Cookies["AdminUSER_COOKIE"];
                if (cookies != null && cookies.HasKeys)
                {
                    string username = cookies["username"];
                    string password = cookies["password"];//此密码为加密密码

                    UsersHelper uhelper = new UsersHelper();
                    Criteria c = new Criteria();
                    c.Add(CriteriaType.Equals, "UserName", username);
                    c.Add(CriteriaType.Equals, "Password", password);
                    c.Add(CriteriaType.Equals, "Flag", 0);
                    List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

                    if (userlist != null && userlist.Count > 0)
                    {
                        System.Web.HttpContext.Current.Session["CurrentAdminUser"] = userlist[0];
                        Users user = userlist[0];
                        return user;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {

                Users user = (Users)System.Web.HttpContext.Current.Session["CurrentAdminUser"];
                if (user == null)
                {
                    //读取cookie中的值
                    HttpCookie cookies = System.Web.HttpContext.Current.Request.Cookies["AdminUSER_COOKIE"];
                    if (cookies != null && cookies.HasKeys)
                    {
                        string username = cookies["username"];
                        string password = cookies["password"];//此密码为加密密码

                        UsersHelper uhelper = new UsersHelper();
                        Criteria c = new Criteria();
                        c.Add(CriteriaType.Equals, "UserName", username);
                        c.Add(CriteriaType.Equals, "Password", password);
                        c.Add(CriteriaType.Equals, "Flag", 0);
                        List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

                        if (userlist != null && userlist.Count > 0)
                        {
                            user = userlist[0];
                        }
                    }
                }

                if (user == null)
                {
                    System.Web.HttpContext.Current.Response.Redirect(GetServerPath());
                }

                System.Web.HttpContext.Current.Session["CurrentAdminUser"] = user;

                return user;
            }
        }

        public static Users GetCurrentUser()
        {
            if (System.Web.HttpContext.Current.Session["CurrentUser"] == null)
            {
                //读取cookie中的值
                HttpCookie cookies = System.Web.HttpContext.Current.Request.Cookies["USER_COOKIE"];
                if (cookies != null && cookies.HasKeys)
                {
                    string username = cookies["username"];
                    string password = cookies["password"];//此密码为加密密码

                    UsersHelper uhelper = new UsersHelper();
                    Criteria c = new Criteria();
                    c.Add(CriteriaType.Equals, "UserName", username);
                    c.Add(CriteriaType.Equals, "Password", password);
                    c.Add(CriteriaType.Equals, "RoleType", 1);
                    c.Add(CriteriaType.Equals, "Flag", 0);
                    List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

                    if (userlist != null && userlist.Count > 0)
                    {
                        //System.Web.HttpContext.Current.Session["CurrentUser"] = userlist[0];
                        Users user = userlist[0];
                        System.Web.HttpContext.Current.Session["CurrentUser"] = user;
                        return user;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {

                Users user = (Users)System.Web.HttpContext.Current.Session["CurrentUser"];


                if (user == null)
                {
                    //读取cookie中的值
                    HttpCookie cookies = System.Web.HttpContext.Current.Request.Cookies["USER_COOKIE"];
                    if (cookies != null && cookies.HasKeys)
                    {
                        string username = cookies["username"];
                        string password = cookies["password"];//此密码为加密密码

                        UsersHelper uhelper = new UsersHelper();
                        Criteria c = new Criteria();
                        c.Add(CriteriaType.Equals, "UserName", username);
                        c.Add(CriteriaType.Equals, "Password", password);
                        c.Add(CriteriaType.Equals, "RoleType", 1);
                        c.Add(CriteriaType.Equals, "Flag", 0);
                        List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

                        if (userlist != null && userlist.Count > 0)
                        {
                            user = userlist[0];
                        }
                    }
                }

                if (user == null)
                {
                    System.Web.HttpContext.Current.Response.Redirect(GetServerPath());
                }

                System.Web.HttpContext.Current.Session["CurrentUser"] = user;

                return user;
            }
        }

        public static string GetServerPath()
        {
            return Config.GetConfig("ServerPath");
        }
    
    }
}